package sfin.swt.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

public class ResourceUtil {

	public static Image getImage(Display display,String name){
		return new Image(display,ApplicationRunner.class.getClassLoader().getResourceAsStream("img/" +  name));
	}
	
	public static String getFileText(String fileName){
		Scanner in = new Scanner(ResourceUtil.class.getResourceAsStream("/"+fileName));
		StringBuilder str = new StringBuilder();
		while(in.hasNextLine())
			str.append(in.nextLine()).append("\n");
		
		in.close();
		return str.toString();
	}
	
	
	public static List<IconCredit> getIconCredits() throws IOException{
		InputStream is = IconCredit.class.getResourceAsStream("/icon-references.txt");
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		
		StringBuilder str = new StringBuilder();
		String temp = null;

		List<IconCredit> credits = new ArrayList<IconCredit>();
		
		IconCredit credit = null;
		
		int count = 0;
		while( (temp = reader.readLine())!=null){
			if(temp == null || temp.trim().equals(""))
				continue;
			if(temp.startsWith("#")){
				count = 0;
				if(credit != null)
					credit.end();
				credit = new IconCredit();
				credits.add(credit.readHeader(temp));
			}else{
				boolean wrap = count == 3;
				credit.addResource(temp, wrap);
				
				if(wrap)
					count = 0;
				
				count++;
			}
			str.append(temp);
		}
		
		reader.close();
	
		return credits;

	}
	
}
